/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.core.provider;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.devoxy.roboeye.R;
import com.devoxy.roboeye.global.log.Log;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 18.06.11
 * Time: 17:44
 * <p/>
 * This class helps open, create, and upgrade the database file.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String TAG = DatabaseHelper.class.getName();

    DatabaseHelper(Context context) {
        super(context, context.getString(R.string.db_name), null, context.getResources().getInteger(R.integer.db_version));
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        StringBuilder sb = new StringBuilder("CREATE TABLE ");
        sb.append(Task.TABLE).append(" (");
        sb.append(Task._ID).append(" INTEGER PRIMARY KEY,");
        sb.append(Task.CREATE_TS).append(" TEXT,");
        sb.append(Task.SOURCE).append(" TEXT,");
        sb.append(Task.TARGET).append(" TEXT,");
        sb.append(Task.TYPE).append(" BOOLEAN,");
        sb.append(Task.STARTED).append(" BOOLEAN,");
        sb.append(Task.FINISHED).append(" BOOLEAN,");
        sb.append(Task.LANGUAGE).append(" TEXT,");
        sb.append(Task.ROTATION).append(" TEXT");
        sb.append(");");
        db.execSQL(sb.toString());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + Task.TABLE);
        onCreate(db);
    }
}
